package io.jboot.fly.directive;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;
import io.jboot.fly.Constants;
import io.jboot.fly.model.Ansers;
import io.jboot.fly.model.Post;
import io.jboot.fly.service.CategoryService;
import io.jboot.fly.service.PostService;
import io.jboot.fly.service.UserService;
import io.jboot.utils.ArrayUtils;
import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;

import javax.inject.Inject;
import java.util.List;

/**
 * @author
 * @version V1.0
 * @Title: 最新公告
 * @Package io.jboot.fly.directive
 */
@JFinalDirective("myPost")
public class MyPost extends JbootDirectiveBase {

    @Inject
    PostService postService;

    @Inject
    UserService userService;

    @Inject
    CategoryService categoryService;

    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        String  userId = getParam("userId",scope);
        List<Record> records=Db.find("select count(*) postCount from post p WHERE p.category_id='0c275ece74fe4c1fbcaa9d3c7438f1f3' and p.user_id='"+userId+"'");
        Long postCount=null;
        for(Record record:records){
            postCount=record.get("postCount");
            scope.setLocal("postCount", postCount==null?0:postCount);
        }
        List<Record> countCountRecords=Db.find("select count(*) as countCount from `comment` c where c.post_id in(select p.id from post p WHERE p.category_id='0c275ece74fe4c1fbcaa9d3c7438f1f3' and p.user_id='"+userId+"')");
        Long countCount=null;
        for(Record record:records){
            countCount=record.get("countCount");
            scope.setLocal("countCount", countCount==null?0:countCount);
        }
        List<Record> huidaCount=Db.find(" select count(*) huidaCount from `comment` c WHERE c.user_id='"+userId+"'");
        Long huidaCount1=null;
        for(Record record:records){
            huidaCount1=record.get("huidaCount");
            scope.setLocal("huidaCount", huidaCount1==null?0:huidaCount1);
        }
        renderBody(env, scope, writer);

//        if (ArrayUtils.isNotEmpty(posts)) {
//            scope.setLocal("posts", posts);
//            renderBody(env, scope, writer);
//        }

    }






    @Override
    public boolean hasEnd() {
        return true;
    }
}
